/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import Persistencia.exceptions.NonexistentEntityException;
import Persistencia.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;
import motoressa.Calle;

/**
 *
 * @author HP
 */
public class CalleJpaController implements Serializable {

    public CalleJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public CalleJpaController() {
    emf=Persistence.createEntityManagerFactory("MotoresSAPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Calle calle) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(calle);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCalle(calle.getCodigo()) != null) {
                throw new PreexistingEntityException("Calle " + calle + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Calle calle) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            calle = em.merge(calle);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = calle.getCodigo();
                if (findCalle(id) == null) {
                    throw new NonexistentEntityException("The calle with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Calle calle;
            try {
                calle = em.getReference(Calle.class, id);
                calle.getCodigo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The calle with id " + id + " no longer exists.", enfe);
            }
            em.remove(calle);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Calle> findCalleEntities() {
        return findCalleEntities(true, -1, -1);
    }

    public List<Calle> findCalleEntities(int maxResults, int firstResult) {
        return findCalleEntities(false, maxResults, firstResult);
    }

    private List<Calle> findCalleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Calle as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Calle findCalle(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Calle.class, id);
        } finally {
            em.close();
        }
    }

    public int getCalleCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Calle as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
